package com.example.rabbitmq.comsumer.delay.config;/**
 * ClassName: RabbitMQConfig <br/> Description: <br/> date: 2022/1/24 14:59<br/>
 *
 * @author chenzhian<br />
 * @version
 */

import static com.example.common.config.RabbitMQConfig.DELAYED_EXCHANGE;
import static com.example.common.config.RabbitMQConfig.DELAYED_QUEUE;
import static com.example.common.config.RabbitMQConfig.DELAYED_ROUTINGKEY;
import static com.example.common.config.RabbitMQConfig.RABBITMQ_DELAYED_EXCHANGE;
import static com.example.common.config.RabbitMQConfig.RABBITMQ_DELAYED_QUEUE;

import java.util.HashMap;
import java.util.Map;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.BindingBuilder.GenericExchangeRoutingKeyConfigurer;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: chenzhian
 * @Date: 2022/1/24 14:59
 * @Description:
 */
@Configuration
public class DelayedQueueConfig {

  //声明延迟交换机
  @Bean
  public CustomExchange delayedExchange(){
    HashMap<String, Object> arguments = new HashMap<>();
    //自定义交换机的类型
    arguments.put("x-delayed-type", "direct");
    /**
     * 交换机名
     * 交换机类型
     * 持久化
     * 自动删除
     */
    return new CustomExchange(DELAYED_EXCHANGE,"x-delayed-message",true,false,arguments);
  }

  /**
   * 声明队列
   * @return
   */
  @Bean
  public Queue delayedQueue(){
    return new Queue(DELAYED_QUEUE);
  }

  //延迟交换机和队列绑定
  @Bean
  public Binding delayedQueueBindingDelayedExchange(Queue delayedQueue,CustomExchange delayedExchange){
    return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYED_ROUTINGKEY).noargs();
  }
}